package net.zjitc.controller;

import net.zjitc.pojo.User;
import net.zjitc.service.Impl.UserServiceImpl;
import net.zjitc.service.UserService;
import net.zjitc.utils.CommonUtils;
import net.zjitc.utils.MailUtils;
import net.zjitc.utils.dispacher.URLMapping;
import org.apache.commons.beanutils.BeanUtils;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * Created by dell on 2017/11/18.
 */
public class UserController {
    UserService service=new UserServiceImpl();
    @URLMapping(url = "/findAllUser")
    public String findAllUser(HttpServletRequest request, HttpServletResponse response)
    {
        List<User> list=service.findAllUser();
        request.setAttribute("list",list);
        return "/admin/user/list.jsp";
    }

    @URLMapping(url = "/userDelete")
    public String userDelete(HttpServletRequest request, HttpServletResponse response)
    {
        String uid=request.getParameter("uid");
        service.userDelete(uid);
        return "/findAllUser";
    }



    @URLMapping(url = "/loginUI")
    public String loginUI(HttpServletRequest request, HttpServletResponse response)
    {
        return "/WEB-INF/user/login.jsp";
    }
    @URLMapping(url = "/registerUI")
    public String registerUI(HttpServletRequest request, HttpServletResponse response)
    {
        return "/WEB-INF/user/regist.jsp";
    }



    @URLMapping(url = "/userRegister")
    public String userRegister(HttpServletRequest request, HttpServletResponse response)
    {
        String inputCode=request.getParameter("code");
        String trueCode= (String) request.getSession().getAttribute("validatecCode");
        if(!inputCode.equalsIgnoreCase(trueCode))
        {
            request.setAttribute("codeMsg","验证码错误");
            return "/WEB-INF/user/regist.jsp";
        }
        User user=new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
            user.setCode(CommonUtils.getUUID());
            service.userRegister(user);
            MailUtils.sendMail(user.getCode(),user.getEmail());
        } catch (Exception e) {
            e.printStackTrace();
        }
        request.setAttribute("showMsg","注册成功,请前往邮箱激活账号");
        return "/WEB-INF/user/msg.jsp";
    }


    @URLMapping(url = "/activeCode")
    public String activeCode(HttpServletRequest request, HttpServletResponse response)
    {
        String code=request.getParameter("code");

        User user=service.findByCode(code);
        if(user!=null)
        {
            user.setCode(null);
            user.setState(1);
            service.update(user);
            request.setAttribute("showMsg","激活成功，请登录");
        }else{
            request.setAttribute("showMsg","激活失败,已激活或是尚未注册");
        }
        return "/WEB-INF/user/msg.jsp";
    }


    @URLMapping(url = "/userLogin")
    public String userLogin(HttpServletRequest request, HttpServletResponse response)
    {
        String inputCode=request.getParameter("code");
        String trueCode= (String) request.getSession().getAttribute("validatecCode");
        if(!inputCode.equalsIgnoreCase(trueCode))
        {
            request.setAttribute("codeMsg","验证码错误");
            return "/WEB-INF/user/login.jsp";
        }
        User user=new User();
        try {
            BeanUtils.populate(user,request.getParameterMap());
            List<User> list=service.findAllUser();

            for(User u:list)
            {
                if(u.getUsername().equals(user.getUsername())&&u.getPassword().equals(user.getPassword()))
                {
                    if(u.getState()==0)
                    {
                        request.setAttribute("showMsg","尚未激活");
                        return "/WEB-INF/user/login.jsp";
                    }else{
                        HttpSession session=request.getSession();
                        session.setAttribute("user",u);
                        return "/index";
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        request.setAttribute("showMsg","用户名或是密码错误");

        return "/WEB-INF/user/login.jsp";
    }
    @URLMapping(url = "/userLogout")
    public String userLogout(HttpServletRequest request, HttpServletResponse response)
    {
        HttpSession session=request.getSession();
        session.removeAttribute("user");
        return "/WEB-INF/user/login.jsp";
    }


    @URLMapping(url = "/checkUsername")
    public void checkUsername(HttpServletRequest request, HttpServletResponse response)
    {

        response.setContentType("text/text");
        response.setCharacterEncoding("utf-8");
        String username = request.getParameter("username");
        try {
            response.getWriter().print(service.checkUsername(username));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
